package com.onemileshop.updater;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.onemileshop.utils.UtiDialogsFactory;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;

public class TaskCheckVersion extends AsyncTask<Void, Void, Void>
{
   String versionName;
   Context context;
   UpdateInfo info;

   public TaskCheckVersion(String _versionName,Context _context)
   {
	  versionName=_versionName;
	  context=_context;
   }
   
   @Override
   protected void onPreExecute()
   {   
	  super.onPreExecute();
   }

   @Override
   protected Void doInBackground(Void... params)
   {
	   

//	  String path = UrlParams.URLCheckNewVersion;
	  URL url;
	  HttpURLConnection conn = null;
	  InputStream is = null;
	  try
	  {
		 url = new URL("");
		 conn = (HttpURLConnection) url.openConnection();
		 conn.setConnectTimeout(5000);
		 is = conn.getInputStream();
		 info = GetUpdateInfo(is);
	  }
	  catch (MalformedURLException e)
	  {
		  
		 e.printStackTrace();
	  }
	  catch (IOException e)
	  {
		  
		 e.printStackTrace();
	  }
	  finally
	  {
		 if (is != null)
		 {
			try
			{
			   is.close();
			}
			catch (IOException e)
			{
			    
			   e.printStackTrace();
			}
		 }
	  }
	  return null;
   }

   @Override
   protected void onPostExecute(Void result)
   {
	   
	  try
	  {
		 if (info.getVersion().equals(versionName))
		 {
			Log.i("DEBUG", "版本号相同无需升级");
		 }
		 else
		 {
			Log.i("DEBUG", "版本号不同 ,提示用户升级 ");
			DialogInterface.OnClickListener clickListener=new OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				   
				  TaskDownloadNewApp taskDownloadNewApp=new TaskDownloadNewApp(context, info.getUrl());
				  taskDownloadNewApp.execute((Void)null);
				  dialog.dismiss();
			   }
			};
            UtiDialogsFactory.getInstance().ShowOKCancelAlertDialog(context,"发现新版本", info.getDescription(), clickListener);
		 }
	  }
	  catch (Exception e)
	  {
		 // 待处理
		 e.printStackTrace();
	  }
   }

   @Override
   protected void onCancelled()
   {
	   
	  super.onCancelled();
   }

   public UpdateInfo GetUpdateInfo(InputStream is)
   {
	  XmlPullParser parser = Xml.newPullParser();
	  UpdateInfo infos = new UpdateInfo();// 实体
	  int type;
	  try
	  {
		 parser.setInput(is, "utf-8"); // 设置解析的数据源
		 type = parser.getEventType();
		 while (type != XmlPullParser.END_DOCUMENT)
		 {
			switch (type)
			{
			   case XmlPullParser.START_TAG:
				  if ("version".equals(parser.getName()))
				  {
					 infos.setVersion(parser.nextText()); // 获取版本号
				  }
				  else if ("url".equals(parser.getName()))
				  {
					 infos.setUrl(parser.nextText()); // 获取要升级的APK文件
				  }
				  else if ("description".equals(parser.getName()))
				  {
					 infos.setDescription(parser.nextText()); // 获取该文件的信息
				  }
				  break;
			}
			type = parser.next();
		 }
	  }
	  catch (XmlPullParserException e)
	  {
		  
		 e.printStackTrace();
	  }
	  catch (IOException e)
	  {
		  
		 e.printStackTrace();
	  }
	  return infos;
   }
}
